/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * dlgMapping.java
 *
 * Created on Nov 16, 2011, 12:05:59 AM
 */

package webserver;

import access.FileAccess;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.midi.SysexMessage;
import bussiness.FolderChooser;
import bussiness.WebServerThread;
import com.sun.corba.se.impl.protocol.giopmsgheaders.Message;
import dto.FolderMapping;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author LW
 */
public class dlgMapping extends javax.swing.JDialog {
    private JTable _tbl;
    private Vector _tblRecord;
    private Vector _tblTitle;


    /** Creates new form dlgMapping */
    public dlgMapping(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public dlgMapping(java.awt.Frame parent, boolean modal, JTable table, Vector tblRecord, Vector tblTitle) {
        super(parent, modal);
        initComponents();
        _tbl = table;
        _tblRecord = tblRecord;
        _tblTitle = tblTitle;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jbtnMapping = new javax.swing.JButton();
        jbtnClose = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        lbName = new javax.swing.JLabel();
        jtxtName = new javax.swing.JTextField();
        jtxtPath = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jbtnChoose = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(webserver.WebServerApp.class).getContext().getResourceMap(dlgMapping.class);
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel2.border.title"))); // NOI18N
        jPanel2.setName("jPanel2"); // NOI18N

        jbtnMapping.setText(resourceMap.getString("jbtnMapping.text")); // NOI18N
        jbtnMapping.setName("jbtnMapping"); // NOI18N
        jbtnMapping.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jbtnMappingMousePressed(evt);
            }
        });

        jbtnClose.setText(resourceMap.getString("jbtnClose.text")); // NOI18N
        jbtnClose.setName("jbtnClose"); // NOI18N
        jbtnClose.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jbtnCloseMousePressed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jbtnMapping)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 174, Short.MAX_VALUE)
                .addComponent(jbtnClose)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnMapping)
                    .addComponent(jbtnClose))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel1.border.title"))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        lbName.setText(resourceMap.getString("lbName.text")); // NOI18N
        lbName.setName("lbName"); // NOI18N

        jtxtName.setText(resourceMap.getString("jtxtName.text")); // NOI18N
        jtxtName.setName("jtxtName"); // NOI18N

        jtxtPath.setEditable(false);
        jtxtPath.setText(resourceMap.getString("jtxtPath.text")); // NOI18N
        jtxtPath.setName("jtxtPath"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jbtnChoose.setText(resourceMap.getString("jbtnChoose.text")); // NOI18N
        jbtnChoose.setName("jbtnChoose"); // NOI18N
        jbtnChoose.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jbtnChooseMousePressed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbName)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jbtnChoose)
                    .addComponent(jtxtPath, javax.swing.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE)
                    .addComponent(jtxtName, javax.swing.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbName)
                    .addComponent(jtxtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jtxtPath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(26, 26, 26)
                .addComponent(jbtnChoose)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnCloseMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbtnCloseMousePressed
        // TODO add your handling code here:
        dispose();
}//GEN-LAST:event_jbtnCloseMousePressed

    private void jbtnChooseMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbtnChooseMousePressed
        File directoryChoice = FolderChooser.getDirectoryChoice(this, "", "Chọn thư mục mapping");
        //System.out.println(directoryChoice.getPath());
        String folderPath = directoryChoice.getPath();        
        jtxtPath.setText(folderPath);
    }//GEN-LAST:event_jbtnChooseMousePressed

    private void jbtnMappingMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbtnMappingMousePressed
        // TODO add your handling code here:
        //checking mapping folder
        if(jtxtPath.getText().equals("")){
            JOptionPane.showMessageDialog(null,"Chưa chọn thư mục", "Error", 1);
            jbtnChoose.requestFocusInWindow();
            return;
        } //checking name
        else if(jtxtName.getText().trim().equals("") || jtxtName.getText().contains(" "))
        {
            JOptionPane.showMessageDialog(null,"Tên folder mapping không được có khoảng trắng", "Error", 1);
            jtxtName.requestFocusInWindow();
            return;
        }else{
            FileAccess facc = new FileAccess();
            //checking file is exists
            String isEx = "mapping/" + jtxtName.getText();
            if(facc.exists(isEx)){ //file is exists
                //
                int n = JOptionPane.showConfirmDialog(this, "Tên mapping này đã có, bạn có chắc muốn ghi đè?",
                                                        "Confirm",  JOptionPane.YES_NO_OPTION);
                if(n == 0){
                    //yes, write data
                }else{
                    return;
                }
            }
            //write data
            FolderMapping fm = new FolderMapping(jtxtName.getText().trim(), jtxtPath.getText());

            if(facc.writeToFile(jtxtName.getText().trim(), fm)){
                //success
                JOptionPane.showMessageDialog(null,"Đã mapping thư mục này", "Success", 1);

                Vector record = new Vector();
                record.add(jtxtName.getText());
                record.add(jtxtPath.getText());

                _tblRecord.add(record);
                _tbl.setModel(new DefaultTableModel(_tblRecord, _tblTitle));

                //set text = null
                jtxtName.setText("");
                jtxtPath.setText("");
            }
        }
    }//GEN-LAST:event_jbtnMappingMousePressed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                dlgMapping dialog = new dlgMapping(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JButton jbtnChoose;
    private javax.swing.JButton jbtnClose;
    private javax.swing.JButton jbtnMapping;
    private javax.swing.JTextField jtxtName;
    private javax.swing.JTextField jtxtPath;
    private javax.swing.JLabel lbName;
    // End of variables declaration//GEN-END:variables

}
